package com.oocl.crm.system;

import java.util.Random;
import java.util.Scanner;

import com.oocl.crm.command.impl.AddCommand;
import com.oocl.crm.command.impl.DeleteCommand;
import com.oocl.crm.command.impl.HelpCommand;
import com.oocl.crm.command.impl.LookUpCommand;
import com.oocl.crm.command.impl.OrderCommand;
import com.oocl.crm.command.impl.QuitCommand;
import com.oocl.crm.command.impl.UpdateCommand;
import com.oocl.crm.db.CrmUserStore;
import com.oocl.crm.pojo.User;
import com.oocl.crm.service.impl.AddBusinessService;
import com.oocl.crm.service.impl.DeleteBusinessService;
import com.oocl.crm.service.impl.HelpBusinessService;
import com.oocl.crm.service.impl.LookUpBusinessService;
import com.oocl.crm.service.impl.OrderBusinessService;
import com.oocl.crm.service.impl.QuitBusinessService;
import com.oocl.crm.service.impl.UpdateBusinessService;
import com.oocl.crm.util.PrintUtil;
import com.oocl.crm.util.StringUtil;

/**
 * 用户管理系统
 * @author GANAB
 *
 */
public class CrmSystem {
	//数据仓库
	private CrmUserStore userDB = CrmUserStore.getInstance();
	
	//实例化业务对象---这里最好用反射去实例化。这里不展示了
	private AddBusinessService addService = new AddBusinessService();
	private DeleteBusinessService deleteService = new DeleteBusinessService();
	private UpdateBusinessService updateService = new UpdateBusinessService();
	private LookUpBusinessService lookUpService = new LookUpBusinessService();
	private OrderBusinessService orderService = new OrderBusinessService();
	private HelpBusinessService helpService = new HelpBusinessService();
	private QuitBusinessService quitService = new QuitBusinessService();
	
	public CrmSystem() {
		initBaseUser(10);//初始化10个用户
	}
	
	private void initBaseUser(int num){
		User user = null;
		for(int i = 0; i < num; i++){
			user = new User();
			user.setId(this.userDB.getAutoId());
			user.setName("UserName"+i);
			user.setSex((new Random().nextBoolean())?"0":"1");
			user.setBirthday("19950422");
			user.setCall("110");
			user.setAddress("ZHA");
			userDB.add(user);
		}
	}
	
	/**
	 * 系统接收
	 * @param params
	 * @return
	 */
	public String accept(String params){
		String result = null;
		try {
			String[] paramsArray = StringUtil.split(params);//取出指令和后续参数
			char command = paramsArray[0].charAt(0);
			switch (command) {
			case 'A':
				result = addService.service(new AddCommand(paramsArray[1]));
				break;
			case 'D':
				result = deleteService.service(new DeleteCommand(paramsArray[1]));
				break;
			case 'U':
				result = updateService.service(new UpdateCommand(paramsArray[1]));
				break;
			case 'L':
				result = lookUpService.service(new LookUpCommand(paramsArray[1]));
				break;
			case 'O':
				result = orderService.service(new OrderCommand(paramsArray[1]));
				break;
			case 'H':
				result = helpService.service(new HelpCommand(paramsArray[1]));
				break;
			case 'Q':
				result = quitService.service(new QuitCommand(paramsArray[1]));
				break;
			default:
				result = "No Such command!";
				break;
			}
			
			if(result != null){
				return result;
			}
		} catch (Exception e) {
			return "[Error]:"+e.getMessage();
		}
		return null;
	}
	
	/**
	 * 启动输入 
	 */
	public void input(){
		Scanner scan = new Scanner(System.in);
		//输出帮助说明
		System.out.println(StringUtil.helpDoc());
		while(true){
			String params = scan.nextLine();
			String result = accept(params);	
			//如果返回quit，退出
			if("quit".equals(result)){
				System.out.println("System quit. Bye.");
				scan.close();
				break;
			}
			//统一输出
			PrintUtil.print(result);
		}
	}
	
}
